package com.jumi.microservice.config;


import cn.hutool.core.util.ObjectUtil;
import com.jm.member.common.jwt.ignore.IgnoreProperties;
import com.jm.member.common.redis.config.FastJson2JsonRedisSerializer;
import com.jm.member.common.redis.service.RedisService;
import com.jumi.microservice.dto.reponse.api.AreaResponse;
import com.jumi.microservice.entity.JmArea;
import com.jumi.microservice.entity.JmUserLevelConfig;
import com.jumi.microservice.service.IJmAreaService;
import com.jumi.microservice.service.IJmUserLevelConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Configuration
@DependsOn("ignorePostProcess")
public class RedisInit {

    public static String REDIS_LEVEL_CONFIG="levelConfig";
    public static String REDIS_AREA_CACHE="areaCache";

    @Autowired
    RedisService redisService;

    @Autowired
    IJmUserLevelConfigService levelConfigService;

    @Autowired
    IJmAreaService areaService;

    @PostConstruct
    public void init(){
        initIgnoreUrl();
        initLevelConfig();
        initArea();
    }
    public void initIgnoreUrl(){
        System.err.println("启动添加忽略接口至Redis----------");
        List<String> ignoreUrls = IgnoreProperties.ignoreUrls;//本项目
        List<Object> cacheList = redisService.getCacheList(IgnoreProperties.REDIS_IGNORE_KEY);//redis中所有的白名单
        List<Object>  newIgnoreUrls=new ArrayList<>();
        for (String ignoreUrl : ignoreUrls) {
            if(!cacheList.contains(ignoreUrl)){
                newIgnoreUrls.add(ignoreUrl);
            }
        }
        if(newIgnoreUrls.size()>0){
            redisService.setCacheList(IgnoreProperties.REDIS_IGNORE_KEY,newIgnoreUrls);
        }
    }

    public void initLevelConfig(){
        System.err.println("启动添加会员级别配置至Redis----------");
        List<JmUserLevelConfig> list = levelConfigService.list();
        Map<Integer, List<JmUserLevelConfig>> collect = list.stream().collect(Collectors.groupingBy(JmUserLevelConfig::getId));
        redisService.setCacheObject(REDIS_LEVEL_CONFIG,collect);
    }

    /**
     * 初始化全国省市区
     */
    public void initArea(){
        System.err.println("初始化省市区至Redis-------");

        List<AreaResponse> areaResponses = redisService.getCacheObject(REDIS_AREA_CACHE);
        if(ObjectUtil.isEmpty(areaResponses)){
            List<JmArea> list = areaService.list();
            Map<Integer, List<JmArea>> areaMap = list.stream().collect(Collectors.groupingBy(JmArea::getAreaParentId));
            List<JmArea> jmAreas = areaMap.get(0);
            if(ObjectUtil.isNotEmpty(jmAreas)){
                areaResponses=new ArrayList<>(jmAreas.size());
                for (JmArea jmArea : jmAreas) {
                    AreaResponse response=new AreaResponse();
                    BeanUtils.copyProperties(jmArea,response);
                    areaResponses.add(response);
                    setAreaResponse(areaMap,response);
                }
               redisService.setCacheObject(REDIS_AREA_CACHE,areaResponses);
            }
        }
    }

    public void setAreaResponse(Map<Integer, List<JmArea>> areaMap,AreaResponse response){
        Integer areaId = response.getAreaId();
        List<JmArea> jmAreas = areaMap.get(areaId);
        if(ObjectUtil.isNotEmpty(jmAreas)){
            List<AreaResponse> areaResponses=new ArrayList<>(jmAreas.size());
            for (JmArea jmArea : jmAreas) {
                AreaResponse r=new AreaResponse();
                BeanUtils.copyProperties(jmArea,r);
                areaResponses.add(r);
                setAreaResponse(areaMap,r);
            }
            response.setAreaResponseList(areaResponses);
        }
    }
}
